package club.vann.kafka.mock.service;

import club.vann.kafka.mock.kafka.KafkaProducer;
import club.vann.kafka.mock.kafka.KafkaTopicFactory;
import com.cyberx.lunyu.Framework;
import com.cyberx.lunyu.MarketPriceSummaryOuterClass;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class MarketPriceService {
  private final KafkaProducer kafkaProducer;

  /** 推送市场价格数据。 */
  public void marketPrice() {
    MarketPriceSummaryOuterClass.MarketPriceSummary marketPriceSummary = build();
    this.kafkaProducer.send(
        KafkaTopicFactory.MARKET_PRICE_SUMMARY_OUTPUT, marketPriceSummary.toByteArray());
  }

  private MarketPriceSummaryOuterClass.MarketPriceSummary build() {
    MarketPriceSummaryOuterClass.MarketPrice btcMarketPrice =
        build("BINANCE_SPOT", "BTC/USDT", 62000.12);
    MarketPriceSummaryOuterClass.MarketPrice ethMarketPrice =
        build("BINANCE_SPOT", "ETH/USDT", 3200.25);
    return MarketPriceSummaryOuterClass.MarketPriceSummary.newBuilder()
        .setSymbol("NOPAIR-CexMarketPriceSummaryAlpha-26c96162")
        .setErrorCode(Framework.ErrorCode.newBuilder().build())
        .setTimestamp(Framework.TimestampRecord.newBuilder().build())
        .putPriceMap("BTC/USDT", btcMarketPrice)
        .putPriceMap("ETH/USDT", ethMarketPrice)
        .build();
  }

  private MarketPriceSummaryOuterClass.MarketPrice build(
      String venue, String ticker, Double price) {
    return MarketPriceSummaryOuterClass.MarketPrice.newBuilder()
        .setVenue(venue)
        .setTicker(ticker)
        .setBid(price - 10)
        .setAsk(price + 10)
        .setTimestamp(System.currentTimeMillis())
        .build();
  }
}
